package com.shemy.controller;

import com.shemy.entity.User;
import com.shemy.service.UserService;
import com.shemy.utils.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description 用户控制器
 * @Author Dzsom
 * @Date 2019-04-03 23:39
 * @Encoding UTF-8
 * @Version 1.0
 **/
@RestController
@RequestMapping("/user")
@Api(tags = "user",description = "用户接口")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 查询全部用户
     * @return Result
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询全部用户")
    public Result list() {
        List<User> users = userService.listAll();
        return new Result(true,200,"查询成功！",users);
    }

    /**
     * 根据Id查询
     * @param id 用户id
     * @return Result
     */
    @GetMapping("/getUser")
    @ApiOperation(value = "根据Id查询")
    @ApiImplicitParam(paramType = "query", name = "id", value = "用户ID", required = true, dataType = "long")
    public Result findById(long id){
        User user = userService.findById(id);
        return new Result(true,200,"查询成功！",user);
    }

    /**
     * 新增
     * @param user
     * @return
     */
    @PostMapping("/add")
    @ApiOperation(value = "新增")
    @ApiImplicitParam(paramType = "body", name = "user", value = "用户", required = true, dataType = "user")
    public Result save(@RequestBody User user){
        int res = userService.save(user);
        if (res>0){
            return new Result(true,200,"添加成功！");
        }
        return new Result(false,400,"添加失败！");
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping("/delete")
    @ApiOperation(value = "根据Id删除")
    @ApiImplicitParam(paramType = "query", name = "id", value = "用户ID", required = true, dataType = "long")
    public Result delete(long id){
        int res=userService.deleteById(id);
        if (res>0){
            return new Result(true,200,"删除成功！");
        }
        return new Result(false,400,"删除失败！");
    }

    /**
     * 更新
     * @param user
     * @return
     */
    @PutMapping("/update")
    @ApiOperation(value = "更新")
    @ApiImplicitParam(paramType = "body", name = "user", value = "用户", required = true, dataType = "user")
    public Result update(@RequestBody User user){
        int res=userService.update(user);
        if (res>0){
            return new Result(true,200,"更新成功！");
        }
        return new Result(false,400,"更新失败！");
    }
}
